Imports MIMS.WRS.Entities
Imports MIMS.WRS.Business

Public Class TerritoryDetail1
    Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents ltrMessage As System.Web.UI.WebControls.Literal
    Protected WithEvents txtDescription As System.Web.UI.WebControls.TextBox
    Protected WithEvents Requiredfieldvalidator2 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents btnOK As System.Web.UI.WebControls.Button
    Protected WithEvents btnDelete As System.Web.UI.WebControls.Button
    Protected WithEvents btnCancel As System.Web.UI.WebControls.Button
    Protected WithEvents ddlCountry As System.Web.UI.WebControls.DropDownList
    Protected WithEvents reqvCountry As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents txtIDNumber As System.Web.UI.WebControls.TextBox
    Protected WithEvents chkShowHDPricingOnIDS As System.Web.UI.WebControls.CheckBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected mainObj As Territory = Nothing

    Protected Property ReferrerUrl() As String
        Get
            Return Convert.ToString(ViewState("ReferrerUrl"))
        End Get
        Set(ByVal Value As String)
            ViewState("ReferrerUrl") = Value
        End Set
    End Property

    Protected ReadOnly Property IsAddNew() As Boolean
        Get
            Return RequestContext.TerritoryID = 0
        End Get
    End Property

    Private Function BuildObject() As Boolean
        If IsAddNew Then
            mainObj = New Territory
        Else
            mainObj = TerritoryService.GetTerritory(RequestContext.TerritoryID)
            If mainObj Is Nothing Then
                Utilities.ShowMessagePage("Failed to load requested territory. It may be deleted or not found")
                Return False
            End If
        End If

        Return True
    End Function

    Private Sub UpdateData(ByVal bSave As Boolean)
        If (bSave) Then
            mainObj.Description = txtDescription.Text
            mainObj.CountryID = Convert.ToInt32(ddlCountry.SelectedItem.Value)
            If (txtIDNumber.Text.Length > 0) Then
                mainObj.IDNumber = Convert.ToInt32(txtIDNumber.Text)
            Else
                mainObj.IDNumber = Nulls.NullInteger
            End If
            mainObj.ShowHDPricingOnIDS = chkShowHDPricingOnIDS.Checked
        Else
            txtDescription.Text = mainObj.Description
            If (mainObj.IDNumber <> Nulls.NullInteger) Then
                txtIDNumber.Text = mainObj.IDNumber.ToString
            Else
                txtIDNumber.Text = ""
            End If

            chkShowHDPricingOnIDS.Checked = mainObj.ShowHDPricingOnIDS
            Try
                ddlCountry.SelectedIndex = ddlCountry.Items.IndexOf(ddlCountry.Items.FindByValue(mainObj.CountryID.ToString()))
            Catch ex As Exception
            End Try
        End If

    End Sub
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If RequestContext.CurrentUser.IS_ADMIN = False Then
            btnDelete.Visible = False
        End If

        If Not IsPostBack Then
            ReferrerUrl = Utilities.GetReferrerUrl("TerritoryList.aspx")

            ddlCountry.DataSource = CountryService.GetCountries(CountryColumns.CountryCode, MyFramework.OrderDirection.ASC)
            ddlCountry.DataTextField = CountryColumns.CountryName.ToString()
            ddlCountry.DataValueField = CountryColumns.CountryID.ToString()
            ddlCountry.DataBind()
            ddlCountry.Items.Insert(0, New ListItem("-- select country --", Nulls.NullInteger.ToString()))

            reqvCountry.InitialValue = Nulls.NullInteger.ToString()

            If IsAddNew Then
                btnDelete.Visible = False
            Else
                If (BuildObject()) Then
                    UpdateData(False)
                End If
                btnDelete.Attributes("onclick") = "return confirm('Are you sure you want to delete?');"
                btnDelete.Visible = True
            End If
        End If
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If (Page.IsValid) Then
            If (BuildObject()) Then
                UpdateData(True)
                If IsAddNew Then                   
                    TerritoryService.CreateTerritory(mainObj)
                Else                    
                    TerritoryService.UpdateTerritory(mainObj)
                End If

                Response.Redirect(ReferrerUrl)
            End If
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect(ReferrerUrl)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        TerritoryService.DeleteTerritory(RequestContext.TerritoryID)
        Response.Redirect(ReferrerUrl)
    End Sub

End Class
